首页 > 编程笔记

Python随机函数(4个)

在 Python 的 NumPy 库中,可以通过 numpy.random 模块中的相关函数获取元素为随机数的数组对象。

1) rand()函数

rand() 函数用于获取指定形状的数组对象,其元素为大于或等于 0 且小于 1 的随机浮点数,其语法格式如下:
rand(*dn)
其中,参数 dn 表示数组对象的形状,示例代码如下:
import numpy as np
#一维数组对象
a=np.random.rand(10)
print(a)
print('========================')
#二维数组对象
b=np.random.rand(3,5)
print(b)
print('========================')
#三维数组对象
c=np.random.rand(2,3,4)
print(c)
运行结果为:
[0.43582529 0.70352278 0.50398772 0.84155759 0.92999583 0.07195663
0.34105594 0.53130844 0.22896855 0.71265797]
========================
[[0.71262211 0.44289437 0.90123382 0.02173123 0.40165761]
[0.95480974 0.23556969 0.08227148 0.65368069 0.53130844]
[0.71265797 0.34105594 0.53130844 0.22896855 0.43582529]]
========================
[[[0.71262211 0.44289437 0.90123382 0.02173123]
  [0.95480974 0.23556969 0.08227148 0.65368069]
  [0.71265797 0.34105594 0.53130844 0.22896855]]

[[0.34105594 0.53130844 0.22896855 0.43582529]
  [0.50398772 0.84155759 0.92999583 0.07195663]
  [0.02173123 0.40165761 0.71262211 0.44289437]]]

2) randint()函数

randint() 函数用于获取指定形状的数组对象,其元素为大于或等于给定最小值且小于给定最大值的随机整数,其语法格式如下:
randint(low,high,size,dtype)

【实例】
import numpy as np
#一维数组对象
a=np.random.randint(5,size=(3,))
print(a)
print('========================')
#二维数组对象
b=np.random.randint(5,10,size=(3,2))
print(b)
print('========================')
#三维数组对象
c=np.random.randint(10,100,size=(2,3,4))
print(c)
输出结果为:
[3 3 2]
========================
[[5 6]
[7 8]
[5 6]]
========================
[[[11 67 23 84]
  [45 33 27 17]
  [92 10 68 29]]

[[56 34 78 42]
  [88 21 43 76]
  [14 98 69 3]]]

3) randn()函数

randn() 函数用于获取指定形状的数组对象,其元素为标准正态分布中的一个或多个样本值,其语法格式如下:
randn(*dn)
其中,参数 dn 表示数组对象的形状。

【实例】
import numpy as np
#一维数组对象
a=np.random.randn(10)
print(a)
print('========================')
#二维数组对象
b=np.random.randn(3,5)
print(b)
print('========================')
#三维数组对象
c=np.random.randn(2,3,4)
print(c)
运行结果为:
[ 0.72569365  1.46998767 -0.39940237 -0.46888942 -0.97634737  0.03680947
  0.82204717 -0.05343358  0.06512406  0.05150843]
========================
[[ 0.70070358 -0.55208533 -1.49589368  0.03193165  1.06152548]
[-0.43107982  0.42126804  0.08201032 -0.08279027 -0.07126858]
[ 0.22126218  0.35285362 -0.04120962  0.05168553  1.34100008]]
========================
[[[ 1.23192336  0.08187017 -0.34277643  0.25757478]
  [-0.04360385  0.85195167  0.37126974 -0.26102227]
  [-0.34277643  0.25757478  1.23192336  0.08187017]
  [ 0.85195167  0.37126974 -0.04360385  0.08187017]]

[[-0.34277643  0.25757478  1.23192336  0.08187017]
  [ 0.85195167  0.37126974 -0.04360385  0.08187017]
  [ 0.08187017 -0.34277643  0.25757478  1.23192336]
  [ 0.37126974 -0.04360385  0.08187017 -0.34277643]]

[[ 0.85195167  0.37126974 -0.04360385  0.08187017]
  [ 0.08187017 -0.34277643  0.25757478  1.23192336]
  [ 1.23192336  0.08187017 -0.34277643  0.25757478]
  [ 0.37126974 -0.04360385  0.08187017 -0.34277643]]]

4) normal()函数

该函数用于获取指定形状的数组对象,其元素为正态分布中的一个或多个样本值,其语法格式如下:
normal(loc,scale,size)
其中,参数 loc 表示平均值;参数 scale 表示标准差;参数 size 表示数组对象的形状。

【实例】
import numpy as np
#一维数组对象
a=np.random.normal(loc=0.0,scale=1.0,size=(3,))
print(a)
print('========================')
#二维数组对象
b=np.random.normal(loc=0.0,scale=1.0,size=(3,2))
print(b)
print('========================')
#三维数组对象
c=np.random.normal(loc=0.0,scale=1.0,size=(2,3,4))
print(c)
运行结果为:
[ 0.72569365  1.46998767 -0.39940237]
========================
[[ 0.70070358 -0.55208533]
[-0.43107982  0.42126804]
[ 0.22126218  0.35285362]]
========================
[[[ 1.23192336  0.08187017 -0.34277643  0.25757478]
  [-0.04360385  0.85195167  0.37126974 -0.26102227]
  [-0.34277643  0.25757478  1.23192336  0.08187017]]

[[-0.34277643  0.25757478  1.23192336  0.08187017]
  [ 0.85195167  0.37126974 -0.04360385  0.08187017]
  [ 0.08187017 -0.34277643  0.25757478  1.23192336]]]

推荐阅读

副业交流群 关注微信公众号,加入副业交流群,学习变现经验,交流各种打法。